package com.alibaba.citrus.ots.b2b.trade.order.repository.impl;

import com.alibaba.citrus.ots.b2b.trade.order.repository.CreditRepository;
import com.alibaba.cz.base.tool.BeanConverter;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsb2btradecenter.domain.QueryCreditRequest;
import com.epoch.app.otsb2btradecenter.domain.QueryCreditResponse;
import com.epoch.app.otspricecenter.api.creditquery.service.CreditQueryService;
import com.epoch.app.otspricecenter.model.dto.BatchQueryCreditRequest;
import com.epoch.app.otspricecenter.model.dto.CreditReponse;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @description:
 * @author: qianlei
 * @create: 2021/10/29 14:46
 */
@Component
public class CreditRepositoryImpl implements CreditRepository {
    @Autowired
    CreditQueryService creditQueryService;
    @Override
    public QueryCreditResponse queryCredit(QueryCreditRequest request) {
        com.epoch.app.otspricecenter.model.dto.QueryCreditRequest priceQueryCredit = new com.epoch.app.otspricecenter.model.dto.QueryCreditRequest();
        priceQueryCredit.setOrganizationCode(request.getOrganizationCode());
        priceQueryCredit.setChannelCode(request.getChannelCode());
        priceQueryCredit.setItemGroupCode(request.getItemGroupCode());
        priceQueryCredit.setCustomerCode(request.getCustomerCode());
        QueryCreditResponse response = new QueryCreditResponse();
        BatchQueryCreditRequest batch = new BatchQueryCreditRequest();
        batch.setQueryCreditRequestList(Lists.newArrayList(priceQueryCredit));
        Result<List<CreditReponse>> result = creditQueryService.queryCredit(batch);
        if (result.isSuccess() && CollectionUtils.isNotEmpty(result.getResult())){
            CreditReponse creditReponse = result.getResult().get(0);
            response.setOrganizationCode(creditReponse.getOrganizationCode());
            response.setChannelCode(creditReponse.getChannelCode());
            response.setItemGroupCode(creditReponse.getItemGroupCode());
            response.setCustomerCode(creditReponse.getCustomerCode());
            response.setBalance(creditReponse.getBalance());
            response.setCreditFlag(creditReponse.getCreditFlag());
            response.setRiskType(creditReponse.getRiskType());
            response.setMessage(creditReponse.getMessage());
            response.setSuccess(creditReponse.getSuccess());
            return response;
        }
        response.setMessage(result.getMessage());
        response.setSuccess(result.isSuccess());
        return response;
    }
}
